package gui;


import guiCode.viewRequestCode;

import javax.swing.JPanel;

import basic.ClientConsole;
import javax.swing.JTable;

import javax.swing.table.DefaultTableModel;

import javax.swing.JOptionPane;

import javax.swing.JScrollPane;

import javax.swing.JLabel;


import com.sun.rowset.CachedRowSetImpl;


import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.ImageIcon;


public class viewRequestGUI extends JPanel{
	public viewRequestCode vrc;
	private JTable table;
	private  JScrollPane scrollPane;
	private DefaultTableModel model;
	private Integer isApprovedArr[]=new Integer[100];
	public JButton btnBack;
	
	public viewRequestGUI(int x, int y, ClientConsole connection) {
	   super();
	   	vrc=new viewRequestCode(connection);
		initialize( x,  y);
	}
	
	
	private void initialize(int high,int width) {
	       this.setLayout(null);   
	    //   this.setSize(width, high);
	       for(int i=0;i<100;i++)			//Initialize isApprovedArr 
	    	   isApprovedArr[i]=-1;
	    
	       
	       this. add(createScrollPane());
		   JLabel lblNewLabel = new JLabel("Select request and set its status (Approve OR Deny) : ");
		   lblNewLabel.setBounds(10, 58, 696, 54);
		   this.add(lblNewLabel);
		   
 /*************************************CancelBtn**********************************************************/	    
		   JButton btnCancel = new JButton("Cancel");
		   btnCancel.setBounds(160, 325, 98, 26);
		   add(btnCancel);
		   btnCancel.addMouseListener(new MouseAdapter() {
		   
		   	public void mouseClicked(MouseEvent e) {
		   		int reply=JOptionPane.showConfirmDialog(null,"Your last changes will be Cancel! Are you sure?");
			   	   if (reply == JOptionPane.YES_OPTION)
			       {
			        
			   		   for(int i=0;i<table.getRowCount();i++){
			   			   if(isApprovedArr[i]!=null)
			   			   {
			   				   System.out.print(isApprovedArr[i]);
			   				   if(isApprovedArr[i]==1){
			   					   vrc.setRequestStatus(i,1);
			   					   table.setValueAt("", i,3);
			   					   table.setValueAt("ACCEPTED", i, 2);
			   				   }
			   				   if(isApprovedArr[i]==0){
			   					   vrc.setRequestStatus(i,0);
			   					   table.setValueAt("", i,2);
			   					   table.setValueAt("DENY", i,3);
			   				   }
			   				   if(isApprovedArr[i]==2){
			   					   vrc.setRequestStatus(i,2);
			   					   table.setValueAt("", i,2);
			   					   table.setValueAt("", i,3);
			   				   }
			   			   }
			   		   }
			   		JOptionPane.showMessageDialog(null,"All Changes were canceled!");
			       }
			   
		   	  }
		   });
/*************************************End_CancelBtn**********************************************************/
/*************************************RefreshBtn**********************************************************/		   
		   JButton btnNewButton = new JButton("");
		   btnNewButton.addMouseListener(new MouseAdapter() {
		   	@Override
		   	public void mouseClicked(MouseEvent event) {
		   				refreshTable();
		   		}

		   });
		   btnNewButton.setIcon(new ImageIcon("C:\\Users\\Roman\\Desktop\\refresh1.gif"));
		   btnNewButton.setBounds(313, 325, 34, 26);
		   add(btnNewButton);
		   
/*************************************END_RefreshBtn**********************************************************/			   
/*************************************BackBtn**********************************************************/
		   btnBack = new JButton("Back");
		   btnBack.setBounds(0, 0, 98, 26);
		   add(btnBack);
/*************************************End_BackBtn**********************************************************/
		 
	}
	
	private JScrollPane createScrollPane(){
		   scrollPane = new JScrollPane();
		   scrollPane.setBounds(10, 140, 587, 145);
		   scrollPane.setViewportView(createRequestTable());
		   
		   return scrollPane;
	}
	private JTable createRequestTable(){
		   table = new JTable();

		   	model = new DefaultTableModel();
		   	table = new JTable(model);
		    model.addColumn("Request");
	        model.addColumn("Description");
	        model.addColumn("Approved");
	        model.addColumn("Deny");
		 
		   table.getColumnModel().getColumn(0).setPreferredWidth(166);
		   table.getColumnModel().getColumn(1).setPreferredWidth(316);
		   
		   
		   
		   table.setEnabled(false);
		  
		   
		   
		   table.setCellSelectionEnabled(true);
		
/****************************************************************************************/		   
		   setAllRequests(vrc.getAllRequestsFromDB());
			
 /****************************************************************************************/           
		   
		table.addMouseListener(new MouseAdapter() {
			 public void mouseClicked(MouseEvent e) {
			   		table.getValueAt(/*rowNum=*/table.rowAtPoint(e.getPoint()), table.columnAtPoint(e.getPoint()));
			  // 	System.out.print(rowNum);
			   		if(table.columnAtPoint(e.getPoint())==3){
			   		
			   		table.setValueAt("", table.rowAtPoint(e.getPoint()), 2); //delete the ACCEPTED string from the cell  When DENY is selected.
			   		table.setValueAt("DENY", table.rowAtPoint(e.getPoint()), 3);
			   		//isApprovedArr[rowNum]=false;
			   		vrc.setRequestStatus(table.rowAtPoint(e.getPoint()),0);
			   	}
			   	if(table.columnAtPoint(e.getPoint())==2){
			   		
			   		table.setValueAt("", table.rowAtPoint(e.getPoint()), 3); //delete the DENY string from the cell  When ACCEPTED is selected.
			   		table.setValueAt("ACCEPTED", table.rowAtPoint(e.getPoint()), 2);
			   		//isApprovedArr[rowNum]=true;								
			   		vrc.setRequestStatus(table.rowAtPoint(e.getPoint()),1);
			   		}
			   	
			 	}
			  });    
		    
		return table;
	}
	
	/**
	 * update all requests from DB to table.
	 * @param crs
	 */
	public void setAllRequests( CachedRowSetImpl crs ){
		
		try {
			crs.beforeFirst();
			while(crs.next())    //build Rows Dynamically
			{
			    model.insertRow(model.getRowCount(), new Object[]{crs.getString("requestID"),
			        crs.getString("description"),crs.getInt("isApproved"),
			        crs.getString("isApproved")});
			    		
			
		    } 
			crs.beforeFirst();
		
		
			for(int i=0;i<crs.size();i++){
			  	  crs.next();
				  table.setValueAt(crs.getString("requestID"),i,0);
				  table.setValueAt(crs.getString("description"),i,1) ;
				  	if(crs.getString("isApproved").equals("1")){
					  table.setValueAt("ACCEPTED",i,2);
					  table.setValueAt("",i,3);
					  if(isApprovedArr[i]==-1)  //set isApprovedArr array only when it is  first initialization of the table.needed for the REFRESH option
						  isApprovedArr[i]=1;
				  	}
					else if(crs.getString("isApproved").equals("0"))
					{
						  table.setValueAt("DENY",i,3);
						  table.setValueAt("",i,2);
						  if(isApprovedArr[i]==-1)  //set isApprovedArr array only when it is  first initialization of the table.needed for the REFRESH option
							  isApprovedArr[i]=0;
					}
					else if (crs.getString("isApproved").equals("2")){
						table.setValueAt("",i,2);
						table.setValueAt("",i,3);
						if(isApprovedArr[i]==-1)         //set isApprovedArr array only when it is  first initialization of the table.needed for the REFRESH option
							isApprovedArr[i]=2;
						
					}
				  	
				  	
			  } 
			
			}
			catch (SQLException e1) {
				  e1.printStackTrace();
			  }
	 }
	
	/**
	 * delete the current Table and update with Data from DataBase
	 */
	private void refreshTable() 
	{
		while(model.getRowCount() != 0)
			 model.removeRow(model.getRowCount()-1);
		setAllRequests(vrc.getAllRequestsFromDB());
					
	}
}

